package com.作业.Day14;

import java.io.File;

/**
 * 目录下是否存在.jpg的文件，如果有，就输出，如果没有，就提示
 */
public class Homework1 {
    public static void main(String[] args) {
        File file = new File("D:\\Desktop\\粤嵌\\粤嵌java班");
        /**
         * 遍历的是File下一级的文件
         */
       int i=0;
        for (String name : file.list()) {
            char[] bytes = name.toCharArray();
            char aByte = bytes[bytes.length - 4];
            char aByte1 = bytes[bytes.length - 3];
            char aByte2 = bytes[bytes.length - 2];
            char aByte3 = bytes[bytes.length - 1];
            String name1 = "" + aByte+aByte1+aByte2+aByte3;
            if(name1.equals(".jpg")){
                System.out.println(name);
                i+=1;
            }
            /**
             * 判断是否为txt
             */
            if(name1.equals(".txt"))
            {
                //将名字类型转换为StringBuilder
                StringBuilder sb = new StringBuilder(name);
                //在原有的基础上插入数据
                StringBuilder e = sb.insert(bytes.length - 2, "e");
                //将数据转换为String类型
                String s = new String(e);
                System.out.println(s);
                //获取当前目录的路径名
                File file1 = new File(file, name);
                System.out.println(file1.getName());
                //修改后的路径名·
                File file2 = new File(file, s);
                System.out.println(file2.getName());
                //进行替换
                boolean b = file1.renameTo(file2);
                System.out.println(b);
            }
        }
        if(i==0){
            System.out.println("该目录下没有");
        }
    }
}
